//
//  MainListViewController.m
//  YanMusicPlayer
//
//  Created by 1564335 on 15-6-11.
//  Copyright (c) 2015年 胡岩 All rights reserved.
//

#import "MainListViewController.h"
#import "MainListScrollModel.h"
#import "MainListColllectionModel.h"
#import "MainCollectionViewCell.h"
#import "SingerList.h"
#import "SingerMusicList.h"
#import "SearchTableViewController.h"
#import "MainCollectionHeaderView.h"

@interface MainListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate, sendTouch, sendSingerTouch, HeaderViewSize, UISearchBarDelegate, UICollectionViewDelegateFlowLayout>
{
    int picCount;
    int picIndex;
}

@property(nonatomic, assign) CGFloat currentX;

@property(nonatomic, assign) CGFloat currentY;

@property(nonatomic, assign) CGFloat totalDistance;

@property(nonatomic, strong) UIView *singerListView;

@property(nonatomic, strong) UIView *singerMusicListView;

@property(nonatomic, strong) UIView *bgView;

@property(nonatomic, strong) UIPageControl *page;

@property(nonatomic, strong) NSMutableArray *arrayData;

@property(nonatomic, strong) NSMutableArray *arrayDataCollection;

@property(nonatomic, strong) UIScrollView *titleScrollView;

@property(nonatomic, strong) UICollectionView *mainCollectionView;

@property(nonatomic, strong) UILabel *titleLabel;

@property(nonatomic, strong) HearderView *hearderView;

@property(nonatomic, assign) CGFloat startY;

@property(nonatomic, assign) NSInteger theTime;

@property(nonatomic, strong) UISearchController *searchControl;

@property(nonatomic, strong) UISearchBar *searchBar;

@property(nonatomic, strong) SearchTableViewController *searchTableVC;

@property(nonatomic, strong) UIActivityIndicatorView *activity;

@property(nonatomic, strong) NSMutableString *songList;

@property(nonatomic, strong) NSString *linkToList;

@end

@implementation MainListViewController

- (void)sendBegin
{
    [_searchBar resignFirstResponder];
    [_searchBar setShowsCancelButton:NO animated:YES];
    [[CDSideBarController sharedInstance] dismissMenu];
    [UIView animateWithDuration:0.8 animations:^{
        self.singerListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        self.singerMusicListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
    }];
    _startY = (int)_hearderView.frame.origin.y;
}
- (void)sendHeaderViewSize:(CGFloat)height
{
    _totalDistance += height;
    _currentY = height;
    if ((int)(_hearderView.frame.origin.y + height) < (int)_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT) && (int)(_hearderView.frame.origin.y + height) > (int)(74 * (_HEIGHT / _DEFAULT_HEIGHT))) {
        _hearderView.center = CGPointMake(_hearderView.center.x, _hearderView.center.y + height);
        if (_totalDistance > 0 && (int)_startY != (int)(_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT))) {
            
            [HearderView shareInstance].isList = NO;
            [UIView animateWithDuration:0.8 animations:^{
                [HearderView shareInstance].listTableView.alpha = 0.0f;
                [HearderView shareInstance].titleImageView.alpha = 1.0f;
            }];
            
            self.bgView.alpha = 0.7 - 0.7 * (_totalDistance / (_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)));
        } else if ((int)_totalDistance > (int)(-(_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT))) && (int)_startY != (int)(74 * (_HEIGHT / _DEFAULT_HEIGHT))) {
            self.bgView.alpha = 0.7 * (-_totalDistance / (_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)));
        }
    }
    [self.view bringSubviewToFront:_hearderView];
}

- (void)sendOver
{
    if (_theTime == 0 && (int)_startY == (int)(_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)) && _totalDistance < 0) {
        _bgView.userInteractionEnabled = NO;
        _theTime = 1;
    } else if (_theTime == 1 && (int)_startY == (int)(74 * (_HEIGHT / _DEFAULT_HEIGHT)) && _totalDistance > 0) {
        _bgView.userInteractionEnabled = YES;
        _theTime = 0;
    }
    if (_currentY > 0) {
        [UIView animateWithDuration:0.8 animations:^{
           _hearderView.frame = CGRectMake(0, _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
            self.bgView.alpha = 0.0;
        }];
    } else if (_currentY <= 0) {
        [UIView animateWithDuration:0.8 animations:^{
            _hearderView.frame = CGRectMake(0, 74 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
            self.bgView.alpha = 0.7;
        }];
    }
    [self.view bringSubviewToFront:_hearderView];
    _totalDistance = 0;
    _currentY = 0;

}



- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [self.view bringSubviewToFront:_searchBar];
    [searchBar setShowsCancelButton:YES animated:YES];
    for(UIView *cc in [[[_searchBar subviews] lastObject] subviews])
    {
        if([cc isKindOfClass:[UIButton class]])
        {
            UIButton *sbtn = (UIButton *)cc;
            [sbtn setTitle:@"搜 索"  forState:UIControlStateNormal];
        }
    }
    [UIView animateWithDuration:0.8 animations:^{
        _bgView.alpha = 0.7;
    }];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
    _searchTableVC = [[SearchTableViewController alloc] init];
    _searchTableVC.url = _searchBar.text;
    if (![_searchBar.text isEqualToString:@""]) {
        [self presentViewController:_searchTableVC animated:YES completion:^{
            [[CDSideBarController sharedInstance] dismissMenu];
        }];
    }
    [UIView animateWithDuration:0.8 animations:^{
        _bgView.alpha = 0.0;
    }];
    [searchBar resignFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
    _searchTableVC = [[SearchTableViewController alloc] init];
    _searchTableVC.url = _searchBar.text;
    if (![_searchBar.text isEqualToString:@""]) {
        [self presentViewController:_searchTableVC animated:YES completion:^{
            [[CDSideBarController sharedInstance] dismissMenu];
        }];
    }
    [UIView animateWithDuration:0.8 animations:^{
        _bgView.alpha = 0.0;
    }];
    [searchBar resignFirstResponder];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [HearderView shareInstance].isSongListView = NO;
    [HearderView shareInstance].title.alpha = 0;
    _bgView.alpha = 0.0;
    _hearderView = [HearderView shareInstance];
    _hearderView.frame = CGRectMake(0, _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
    [self.view addSubview:_hearderView];
    _hearderView.HeaderViewDelegate = self;
    
    [self.view bringSubviewToFront:_searchBar];
    [self.view bringSubviewToFront:self.bgView];
    [self.view bringSubviewToFront:_singerListView];
    [self.view bringSubviewToFront:_singerMusicListView];
    [self.view bringSubviewToFront:_hearderView];
    
    [HearderView shareInstance].titleImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
    [HearderView shareInstance].titleImageView.center = CGPointMake(37 * (_WIDTH / _DEFAULT_WIDTH), 32 * (_HEIGHT / _DEFAULT_HEIGHT));
    
    [HearderView shareInstance].lastButton.frame = CGRectMake(_WIDTH - 240 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [HearderView shareInstance].startButton.frame = CGRectMake(_WIDTH - 160 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [HearderView shareInstance].nextButton.frame = CGRectMake(_WIDTH - 80 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    
    [HearderView shareInstance].progressSlider.alpha = 0;
    [HearderView shareInstance].likeBtn.alpha = 0;
    [HearderView shareInstance].downloadBtn.alpha = 0;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _WIDTH, _HEIGHT)];
    [self.view addSubview:backgroundImageView];
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *backgroundView = [[UIVisualEffectView alloc] initWithEffect:effect];
    backgroundView.frame = CGRectMake(0, 0, _WIDTH, _HEIGHT);
    [self.view addSubview:backgroundView];
    [backgroundImageView setImage:[UIImage imageNamed:@"bgImage2"]];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT), self.view.frame.size.width - 70, 80)];
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    [self.searchBar sizeToFit];
    self.searchBar.delegate = self;
    [self.view addSubview:_searchBar];
    self.searchBar.alpha = 1;
    _searchBar.placeholder = @"歌名 / 歌手名";
    _searchBar.tintColor = [UIColor whiteColor];
    _searchBar.barTintColor = [UIColor whiteColor];
    
    _hearderView = [HearderView shareInstance];
    _hearderView.frame = CGRectMake(0, _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT - 74 * (_HEIGHT / _DEFAULT_HEIGHT));
    [self.view addSubview:_hearderView];
    _hearderView.HeaderViewDelegate = self;
    [self.view bringSubviewToFront:_hearderView];
    
    // 初始化singerlistview
    self.bgView = [[UIView alloc] initWithFrame:self.view.frame];
    self.bgView.backgroundColor = [UIColor blackColor];
    self.bgView.alpha = 0.0;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self.bgView addGestureRecognizer:tap];
    self.bgView.userInteractionEnabled = YES;
    
    // 歌手列表
    self.singerListView = [[UIView alloc] initWithFrame:CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT))];
    self.singerListView.layer.cornerRadius = 5;
    self.singerListView.layer.masksToBounds = YES;
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.singerListView];
    self.singerListView.backgroundColor = [UIColor colorWithRed:211/255.0 green:218/255.0 blue:252/255.0 alpha:1];
    
    // 歌手音乐列表
    self.singerMusicListView = [[UIView alloc] initWithFrame:CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT))];
    self.singerMusicListView.layer.cornerRadius = 5;
    self.singerMusicListView.layer.masksToBounds = YES;
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.singerMusicListView];
    self.singerMusicListView.backgroundColor = [UIColor whiteColor];
    
    _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:_activity];
    [_activity startAnimating];
    _activity.center = self.view.center;
    
    _linkToList = @"%2C";
    
    self.arrayData = [NSMutableArray array];
    self.arrayDataCollection = [NSMutableArray array];
    __weak typeof(self)weakSelf = self;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[URLPath mainListURL1] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic valueForKey:@"data"];
        for (NSDictionary *dic in array) {
            MainListScrollModel *model = [[MainListScrollModel alloc] initWithDic:dic];
            [self.arrayData addObject:model];
        }
        
        [weakSelf initScrollView];
        [weakSelf downloadCollectionData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败!" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }];
}

- (void)downloadCollectionData
{
    __weak typeof(self)weakSelf = self;
    AFHTTPRequestOperationManager *manager1 = [AFHTTPRequestOperationManager manager];
    manager1.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager1 GET:[URLPath mainListURL2] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic valueForKey:@"data"];
        for (NSDictionary *dic in array) {
            MainListColllectionModel *model = [[MainListColllectionModel alloc] initWithDic:dic];
            [weakSelf.arrayDataCollection addObject:model];
        }
        [weakSelf initCollectionView];
        [weakSelf.activity stopAnimating];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [weakSelf.activity stopAnimating];
    }];
}


- (void)initScrollView
{
    
    self.titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, (NSInteger)(_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH)), 150 * (_HEIGHT / _DEFAULT_HEIGHT))];
//    NSLog(@"%f", self.titleScrollView.frame.size.width);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    self.titleScrollView.pagingEnabled = YES;
    self.titleScrollView.bounces = NO;
    self.titleScrollView.contentOffset = CGPointMake(_titleScrollView.frame.size.width, 0);
    self.titleScrollView.delegate = self;
    self.titleScrollView.tag = 1;
    
    picCount = 0;
    for (MainListScrollModel *model in self.arrayData) {
        if (model.pic != nil) {
            picCount++;
        }
    }
    self.titleScrollView.contentSize = CGSizeMake(_titleScrollView.frame.size.width * (picCount + 2), self.titleScrollView.frame.size.height);
    picIndex = 1;
    for (MainListScrollModel *model in self.arrayData) {
        if (model.pic != nil) {
            UIImageView *titleImage = [[UIImageView alloc] initWithFrame:CGRectMake(picIndex * _titleScrollView.frame.size.width, 0, _titleScrollView.frame.size.width, self.titleScrollView.frame.size.height)];
            [titleImage setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:nil];
            titleImage.userInteractionEnabled = YES;
            titleImage.layer.cornerRadius = 5;
            titleImage.layer.masksToBounds = YES;
            [self.titleScrollView addSubview:titleImage];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage:)];
            [titleImage addGestureRecognizer:tap];
            titleImage.tag = picIndex;
            picIndex++;
        }
    }
    MainListScrollModel *firstModel = [self.arrayData objectAtIndex:self.arrayData.count - 1];
    UIImageView *firstImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.titleScrollView.frame.size.width, self.titleScrollView.frame.size.height)];
    [firstImage setImageWithURL:[NSURL URLWithString:firstModel.pic] placeholderImage:nil];
    firstImage.layer.cornerRadius = 5;
    firstImage.layer.masksToBounds = YES;
    [self.titleScrollView addSubview:firstImage];
    
    MainListScrollModel *lastModel = [self.arrayData objectAtIndex:0];
    UIImageView *lastImage = [[UIImageView alloc] initWithFrame:CGRectMake(_titleScrollView.frame.size.width * (picCount + 1), 0, self.titleScrollView.frame.size.width, self.titleScrollView.frame.size.height)];
    [lastImage setImageWithURL:[NSURL URLWithString:lastModel.pic] placeholderImage:nil];
    lastImage.layer.cornerRadius = 5;
    lastImage.layer.masksToBounds = YES;
    [self.titleScrollView addSubview:lastImage];
    
    
    [self.view bringSubviewToFront:_searchBar];
    [self.view bringSubviewToFront:self.bgView];
    [self.view bringSubviewToFront:_hearderView];
}

- (void)chooseImage:(UITapGestureRecognizer *)tap
{
    [MusicPlayer1 shareInstance].isDownLoad = NO;
    [MusicPlayer1 shareInstance].isDownLoadList = NO;
    [_searchBar resignFirstResponder];
    [_searchBar setShowsCancelButton:NO animated:YES];
    [[CDSideBarController sharedInstance] dismissMenu];
    NSMutableArray *tempArray = [NSMutableArray array];
    for (MainListScrollModel *model in self.arrayData) {
        if (model.pic != nil) {
            [tempArray addObject:model];
        }
    }
//    UIView *tapView = (UIView *)tap;
    MainListScrollModel *scrollModel = [tempArray objectAtIndex:[tap view].tag - 1];
    NSString *path = [[URLPath mainListScrollViewURL]stringByAppendingFormat:@"%@", scrollModel.msg_id];
    
    NSString *parameter = @"%5D&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=7.1.2&active=1&net=2&openudid=806c45eba2515bd74b0d6f00f64b96e461162b39&idfa=E7ECAA4B-8BA1-4BA8-9922-FFD011790A9F&utdid=VYVrXKwYuekDAGdTmMkz53%2By&alf=201200&bundle_id=com.ttpod.music";
    NSString *finalPath = [path stringByAppendingString:parameter];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    _songList = [NSMutableString string];
    [_songList appendFormat:@"http://ting.hotchanson.com/v2/songs/download?song_id="];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:finalPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic valueForKey:@"data"];
        NSDictionary *dic2 = [array firstObject];
        NSArray *songList = dic2[@"songlist"];
        for (NSDictionary *dic in songList) {
            NSString *songId = dic[@"song_id"];
            [_songList appendString:songId];
            [_songList appendString:_linkToList];
        }
        [self initSingerMusicList];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
}

- (void)initSingerMusicList
{
    [UIView animateWithDuration:0.8 animations:^{
        self.singerListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        self.bgView.alpha = 0.0;
    }];
    SingerMusicList *singMusicListView = [[SingerMusicList alloc] initWithFrame:CGRectMake(0, 10 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 320 * (_HEIGHT / _DEFAULT_HEIGHT)) Singername:_songList isScroll:YES];
    for (UIView *view in self.singerMusicListView.subviews) {
        [view removeFromSuperview];
    }
    singMusicListView.touchTableViewDelegate = self;
    [self.singerMusicListView addSubview:singMusicListView];
    [self.view bringSubviewToFront:self.bgView];
    [self.view bringSubviewToFront:self.singerMusicListView];
    [self.view bringSubviewToFront:_hearderView];

    _singerMusicListView.backgroundColor = [UIColor colorWithRed:211/255.0 green:218/255.0 blue:252/255.0 alpha:1];
    [UIView animateWithDuration:0.8 animations:^{
        self.singerMusicListView.frame = CGRectMake(0, 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        self.bgView.alpha = 0.7;
    }];
}

- (void)initCollectionView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake((_WIDTH - 60 * (_WIDTH / _DEFAULT_WIDTH)) / 3, (_WIDTH - 60 * (_WIDTH / _DEFAULT_WIDTH)) / 3);
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15 * (_WIDTH / _DEFAULT_WIDTH), 84 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 144 * (_HEIGHT / _DEFAULT_HEIGHT)) collectionViewLayout:layout];
//    layout.minimumLineSpacing = 0;
    self.mainCollectionView.backgroundColor = [UIColor clearColor];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
//    self.mainCollectionView.
    self.mainCollectionView.showsVerticalScrollIndicator = NO;
    [self.mainCollectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    layout.headerReferenceSize = CGSizeMake(_WIDTH - 30 * (_WIDTH / _DEFAULT_HEIGHT), 170 * (_HEIGHT / _DEFAULT_HEIGHT));
    [_mainCollectionView registerClass:[MainCollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [self.view addSubview:self.mainCollectionView];
    [self.view bringSubviewToFront:_page];
    [self.view bringSubviewToFront:_searchBar];
    [self.view bringSubviewToFront:self.bgView];
    [self.view bringSubviewToFront:_singerMusicListView];
    [self.view bringSubviewToFront:_hearderView];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    MainCollectionHeaderView *headView = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        [headView addSubview:_titleScrollView];
        _page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 115 * (_WIDTH / _DEFAULT_WIDTH), 130 * (_HEIGHT / _DEFAULT_HEIGHT), 80 * (_WIDTH / _DEFAULT_WIDTH), 0)];
        [headView addSubview:_page];
        _page.numberOfPages = picCount;
    }
    return headView;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag == 1) {
        int num = scrollView.contentOffset.x / scrollView.frame.size.width;
        if (num == 0) {
            scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * picCount , 0);
            _page.currentPage = picCount;
        } else if (num == (picCount + 1)) {
            scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
            _page.currentPage = 0;
        } else {
            _page.currentPage = num - 1;
        }
        
    }
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView.tag == 1) {
//        int num = scrollView.contentOffset.x / scrollView.frame.size.width;
//        if (num == 0) {
//            _page.currentPage = picCount;
//        } else if (num == (picCount + 1)) {
//            _page.currentPage = 0;
//        } else {
//            _page.currentPage = num - 1;
//        }
//
//    }
//}



- (void)tap
{
    [UIView animateWithDuration:0.8 animations:^{
        self.singerListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        self.singerMusicListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        self.bgView.alpha = 0.0;
    }];
    [[CDSideBarController sharedInstance] dismissMenu];
    [_searchBar resignFirstResponder];
    [_searchBar setShowsCancelButton:NO animated:YES];
}

// SingerList代理方法

- (void)goBack:(NSString *)SingerName
{
    [_searchBar resignFirstResponder];
    [_searchBar setShowsCancelButton:NO animated:YES];
    [UIView animateWithDuration:0.8 animations:^{
        self.singerListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        self.bgView.alpha = 0.0;
    }];
    [[CDSideBarController sharedInstance] dismissMenu];
    SingerMusicList *singMusicListView = [[SingerMusicList alloc] initWithFrame:CGRectMake(0, 40 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 350 * (_HEIGHT / _DEFAULT_HEIGHT)) Singername:SingerName isScroll:NO];
    for (UIView *view in self.singerMusicListView.subviews) {
        [view removeFromSuperview];
    }
    singMusicListView.touchTableViewDelegate = self;
    [self.singerMusicListView addSubview:singMusicListView];
    [self.view bringSubviewToFront:self.bgView];
    [self.view bringSubviewToFront:self.singerMusicListView];
    [self.view bringSubviewToFront:_hearderView];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.singerListView.frame.size.width, 40 * (_HEIGHT / _DEFAULT_HEIGHT))];
    [_singerMusicListView addSubview:self.titleLabel];
    self.titleLabel.text = [NSString stringWithFormat:@"  %@", SingerName];
//    _titleLabel.backgroundColor = [UIColor colorWithRed:131/255.0 green:162/255.0 blue:244/255.0 alpha:0.3];
    _singerMusicListView.backgroundColor = [UIColor colorWithRed:211/255.0 green:218/255.0 blue:252/255.0 alpha:1];
    [UIView animateWithDuration:0.8 animations:^{
        self.singerMusicListView.frame = CGRectMake(0, 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        self.bgView.alpha = 0.7;
    }];
}

- (void)sendTouchEnd
{
    if (self.currentX < 0 && self.singerListView.frame.origin.x != 0) {
        [UIView animateWithDuration:0.8 animations:^{
            self.singerListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
            self.bgView.alpha = 0.0;
        }];
    } else if (self.currentX > 0 && self.singerListView.frame.origin.x != 0) {
        [UIView animateWithDuration:0.8 animations:^{
            self.singerListView.frame = CGRectMake(0, 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
            self.bgView.alpha = 0.7;
        }];
    } else if (self.singerListView.frame.origin.x == 0) {
        self.bgView.alpha = 0.7;
    }
    self.currentX = 0;
    _totalDistance = 0;
}

- (void)getTouch:(CGFloat)pointX
{
    self.currentX = 0;
    _totalDistance += pointX;
    if (pointX < -5 || pointX > 5) {
        self.currentX = pointX;
    }
    if (self.singerListView.frame.origin.x + pointX <= 0) {
        self.singerListView.center = CGPointMake(self.singerListView.center.x + self.currentX, self.singerListView.center.y);
        if (_totalDistance < 0 && -_totalDistance < (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH))) {
            self.bgView.alpha = 0.7 - 0.7 * (-_totalDistance / (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH)));
        } else if (_totalDistance < (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH))) {
            self.bgView.alpha = 0.7 * (_totalDistance / (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH)));
        }
    }
}


// SingerMusicList 代理方法

- (void)sendTableViewTouchEnd
{
    if (self.currentX < 0 && self.singerMusicListView.frame.origin.x != 0) {
        [UIView animateWithDuration:0.8 animations:^{
            self.singerMusicListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
            self.bgView.alpha = 0.0;
        }];
    } else if (self.currentX >= 0 && self.singerMusicListView.frame.origin.x != 0) {
        [UIView animateWithDuration:0.8 animations:^{
            self.singerMusicListView.frame = CGRectMake(0, 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
            self.bgView.alpha = 0.7;
        }];
    }
    _totalDistance = 0;
    self.currentX = 0;
}

- (void)getTableViewTouch:(CGFloat)pointX
{
    self.currentX = 0;
    _totalDistance += pointX;
    if (pointX < -5 || pointX > 5) {
        self.currentX = pointX;
    }
    if (self.singerMusicListView.frame.origin.x + pointX <= 0 && -_totalDistance < (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH))) {
        self.singerMusicListView.center = CGPointMake(self.singerMusicListView.center.x + self.currentX, self.singerMusicListView.center.y);
        if (_totalDistance < 0) {
            self.bgView.alpha = 0.7 - 0.7 * (-_totalDistance / (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH)));
        } else if (_totalDistance < (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH))) {
            self.bgView.alpha = 0.7 * (_totalDistance / (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH)));
        }
    }
}

- (void)goBackTableView:(NSString *)SingerName number:(NSInteger)number
{
    [MusicPlayer1 shareInstance].isDownLoad = NO;
    [MusicPlayer1 shareInstance].isDownLoadList = NO;
    [_searchBar resignFirstResponder];
    [_searchBar setShowsCancelButton:NO animated:YES];
    [[CDSideBarController sharedInstance] dismissMenu];
    MusicModel *model = [[MusicPlayer1 shareInstance].list objectAtIndex:number];
    [MusicPlayer1 shareInstance].firstPlay = YES;
    [MusicPlayer1 shareInstance].currentMusic = number;
    [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
    [MusicPlayer1 shareInstance].isPlay = YES;
    [[HearderView shareInstance].startButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    [[MusicPlayer1 shareInstance] play];
    [[HearderView shareInstance].listTableView reloadData];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [_searchBar resignFirstResponder];
    [_searchBar setShowsCancelButton:NO animated:YES];
    MainListColllectionModel *model = [self.arrayDataCollection objectAtIndex:indexPath.row];
    SingerList *singerView = [[SingerList alloc] initWithFrame:CGRectMake(0, 40 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 340 * (_HEIGHT / _DEFAULT_HEIGHT)) :model.id_];
    singerView.sendDelegate = self;
    [[CDSideBarController sharedInstance] dismissMenu];
    for (UIView *view in self.singerListView.subviews) {
        [view removeFromSuperview];
    }
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.singerListView.frame.size.width, 40 * (_HEIGHT / _DEFAULT_HEIGHT))];
    self.titleLabel.textColor = [UIColor blackColor];
    [self.singerListView addSubview:self.titleLabel];
    [self.singerListView addSubview:singerView];
    self.titleLabel.text = [NSString stringWithFormat:@"  %@", model.title];
//    _titleLabel.backgroundColor = [UIColor colorWithRed:131/255.0 green:162/255.0 blue:244/255.0 alpha:0.3];
    _singerListView.backgroundColor = [UIColor colorWithRed:211/255.0 green:218/255.0 blue:252/255.0 alpha:1];
    [UIView animateWithDuration:0.8 animations:^{
        self.singerListView.frame = CGRectMake(0, 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        self.bgView.alpha = 0.7;
    }];
    [self.view bringSubviewToFront:self.bgView];
    [self.view bringSubviewToFront:self.singerListView];
    [self.view bringSubviewToFront:_hearderView];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    MainListColllectionModel *model = [self.arrayDataCollection objectAtIndex:indexPath.row];
    [cell setMainListModel:model];
    return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrayDataCollection.count;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
